package stack;


import java.util.Scanner;

//用链表模拟栈
public class LinkedStackDemo {
    public static void main(String[] args) {
        LinkedStack stack = new LinkedStack(5);
        char key = ' ';//接收用户输入
        Scanner scanner = new Scanner(System.in);
        boolean loop = true;
        while (loop) {
            System.out.println("s(show):显示队列");
            System.out.println("e(exit):退出队列");
            System.out.println("a(add):添加数据到队列");
            System.out.println("g(get):从队列获取数据");
            key = scanner.next().charAt(0);//接受的字符
            switch (key) {
                case 's':
                    stack.list();
                    break;
                case 'a':
                    System.out.println("请输入1个数");
                    int n = scanner.nextInt();
                    stack.push(n);
                    break;
                case 'g':
                    try {
                        int num = stack.pop();
                        if (num > 0) {
                            System.out.printf("取出的数据是%d\n", num);
                        }
                    } catch (Exception e) {
                        e.printStackTrace();
                    }
                    break;
                case 'e':
                    scanner.close();
                    loop = false;
                    break;
                default:
                    break;
            }
        }
        System.out.println("程序退出");
    }

    //定义链表
    static class LinkedStack {
        private int maxsize;
        private int top;//栈顶

        public LinkedStack(int maxsize) {
            this.maxsize = maxsize;
        }

        //是否栈满
        public boolean isFull() {
            return top == maxsize ;
        }

        //是否栈空
        public boolean isEmpty() {
            return top == 0;
        }

        //入栈
        public void push(int num) {
            if (isFull()) {
                System.out.println("栈满了，不能入栈。");
                return;
            }
            //头节点不能动
            HeroNode temp = head;
            //遍历找到最后一个节点
            while (true) {
                if (temp.next == null) {
                    break;
                }
                temp = temp.next;
            }
            //指向新的节点
            temp.next = new HeroNode(num);
            top++;
        }

        //出栈
        public int pop() {
            if (isEmpty()) {
                System.out.println("栈空了，不能出栈。");
                return -1;
            }
            //头节点不能动
            HeroNode temp = head;
            int count = 0;
            while (true) {
                if (count == top) {
                    break;
                }
                temp = temp.next;
                count ++;
            }
            top--;
            return temp.no;
        }

        //建立头节点
        private HeroNode head = new HeroNode(0);

        //返回头节点
        public HeroNode getHead() {
            return head;
        }

        //遍历双向链表
        public void list() {
            if (head.next == null) {
                System.out.println("链表为空");
                return;
            }
            //头节点不能动
            HeroNode temp = head.next;
            //遍历找到最后一个节点
            int count = 0;
            while (true) {
                if (count == top) {
                    break;
                }
                System.out.println(temp);
                temp = temp.next;
                count ++;
            }
        }

    }


    //定义节点
    static class HeroNode {
        private int no;//排名
        private HeroNode next;//指向下一个节点

        public HeroNode(int hno) {
            this.no = hno;
        }

        @Override
        public String toString() {
            return "HeroNode{" +
                    "no=" + no +
                    '}';
        }
    }
}
